﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Components;
using Model;
using System.Data;
using System.Collections;

namespace MsDAL
{
    public class Sys5a00DAL
    {
        /// <summary>
        /// 检查页面和控件是否已经存在
        /// </summary>
        /// <param name="pageName">页面</param>
        /// <param name="ctlsName">控件</param>
        /// <returns></returns>
        public bool IsExists(string pageName, string ctlsName)
        {
            string sql = " SELECT 1 FROM Sys5a00 WHERE LOWER(pagename) =LOWER(@pagename) "
                + " AND LOWER(ctlname) =LOWER(@ctlname) ";

            object o = DBHelper.ExecuteScalar(sql, new SqlParameter("@pagename", pageName),
                new SqlParameter("@ctlname", ctlsName));

            return !Validation.IsNull(o);
        }


        public object GetKeyByPageName(string pagename, string ctl)
        {
            string sql = "SELECT listid FROM Sys5a00 WHERE LOWER(pagename) =LOWER(@pagename) AND  LOWER(ctlname) =LOWER(@ctlname)";

            return DBHelper.ExecuteScalar(sql, new SqlParameter("@pagename", pagename),
                new SqlParameter("@ctlname", ctl));
        }

        /// <summary>
        /// 检查页面和控件是否已经存在
        /// </summary>
        /// <param name="pageName">页面</param>
        /// <param name="ctlsName">控件</param>
        /// <returns></returns>
        public Sys5a00 GetModel(string pageName, string ctlsName)
        {
            string sql = " SELECT listid, pagename, ctlname, colsize, lsttime, remark, pagetitle "
                + " FROM Sys5a00 WHERE LOWER(pagename) =LOWER(@pagename) "
                + " AND LOWER(ctlname) =LOWER(@ctlname) ";

            DataTable dt = DBHelper.GetData(sql, new SqlParameter("@pagename", pageName),
                new SqlParameter("@ctlname", ctlsName));

            return (null == dt || dt.Rows.Count == 0) ? null : Sys5a00.GetSys5a00ByDataRow(dt.Rows[0]);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public virtual SqlApp Add(Model.Sys5a00 sys5a00)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Sys5a00(");
            strSql.Append("listid ,pagename ,ctlname ,colsize ,lsttime ,remark, pagetitle )");
            strSql.Append(" values (");
            strSql.Append("@listid ,@pagename ,@ctlname ,@colsize ,@lsttime ,@remark,@pagetitle )");

            SqlParameter[] parameters = {
				new SqlParameter("@listid",SqlDbType.NVarChar),
				new SqlParameter("@pagename",SqlDbType.NVarChar),
				new SqlParameter("@ctlname",SqlDbType.NVarChar),
				new SqlParameter("@colsize",SqlDbType.Int),
				new SqlParameter("@lsttime",SqlDbType.DateTime),
				new SqlParameter("@remark",SqlDbType.NVarChar),
                new SqlParameter("@pagetitle",SqlDbType.NVarChar)
				};
            parameters[0].Value = sys5a00.Listid;
            parameters[1].Value = sys5a00.Pagename;
            parameters[2].Value = sys5a00.Ctlname;
            parameters[3].Value = sys5a00.Colsize;
            parameters[4].Value = sys5a00.Lsttime;
            parameters[5].Value = sys5a00.Remark;
            parameters[6].Value = sys5a00.Pagetitle;

            return new SqlApp(strSql.ToString(), new ArrayList(parameters), false, "数据保存失败！");
        }

        /// <summary>
        /// 得到页面列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetPageList()
        {
            string sql = "SELECT listid,pagename,ctlname,colsize,lsttime,remark, pagetitle "
                + " FROM Sys5a00 ORDER BY pagename ASC, ctlname ASC";

            return DBHelper.GetData(sql);
        }

        public bool Delete(string list)
        {
            //删除明细记录
            string sql = " DELETE FROM Sys5a01 WHERE plistid =@listid ";
            SqlParameter p = new SqlParameter("@listid", list);
            if (DBHelper.DoSql(sql, p))
            {
                sql = " DELETE FROM Sys5a00 WHERE listid =@listid ";
                return DBHelper.DoSql(sql, p);
            }
            else return false;
        }

        //复制模块
        public string Copy(string srclistid, string listid, string name, string size, string ctlname)
        {
            try
            {
                string sql = string.Format("copylayout_sp '{0}','{1}','{2}','{3}','{4}' ",
                       srclistid, listid, name, ctlname, size);

                return DBHelper.DoSql(sql) ? "" : "复制失败！";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
}
